package com.king.common.utils;

import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.king.common.utils.sql.SqlUtil;
import com.king.framework.base.domain.vo.PageDomain;
import com.king.framework.base.domain.vo.TableSupport;

/**
 * 分页工具类
 * 
 * @author king
 */
public class PageUtils extends PageHelper {
	/**
	 * 设置请求分页数据
	 */
	public static void startPage() {
		PageDomain pageDomain = TableSupport.buildPageRequest();
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
		Boolean reasonable = pageDomain.getReasonable();
		PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
	}

	/**
	 * 清理分页的线程变量
	 */
	public static void clearPage() {
		PageHelper.clearPage();
	}
	
	
	public static void startPage(Map<String, Object> searchMap) {
		PageDomain pageDomain = TableSupport.buildPageRequest(searchMap);
		Integer pageNum = pageDomain.getPageNum();
		Integer pageSize = pageDomain.getPageSize();
		String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
		Boolean reasonable = pageDomain.getReasonable();
		if(pageSize != -1){
			PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
		}
	}

}
